package httptool;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import app.Application;

/**
 * Created by Administrator on 2016/5/18/018.
 */

/**
 * A canned request for retrieving the response body at a given URL as a String.
 */
public class MyRequestVolly extends Request<String> {
    private final Response.Listener<String> mListener;
    private Context context;
    /**
     * Creates a new request with the given method.
     *
     * @param method the request {@link Method} to use
     * @param url URL to fetch the string at
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public MyRequestVolly(int method, String url, Response.Listener<String> listener,
                          Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.context = context;
        mListener = listener;
    }

    /**
     * Creates a new GET request.
     *
     * @param url URL to fetch the string at
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public MyRequestVolly(Context context, int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        this(method, url, listener, errorListener);
    }

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        String cookies = SharePrefUtil.getString(Application.getApp(),"Cookie","");
        if(!TextUtils.isEmpty(cookies)){
            HashMap headers = new HashMap();
            headers.put("Cookie",cookies);
            return headers;
        }
        return super.getHeaders();
    }
    //设置超时时间
    @Override
    public RetryPolicy getRetryPolicy() {
        RetryPolicy retryPolicy = new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        int count = retryPolicy.getCurrentTimeout();
        if(count>5){
            ShowDialog.showToast(Application.getApp(),"请检查网络");
        }
        return retryPolicy;
    }

    /**
     * 重写以解决乱码问题
     */
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed=null;
        try {
            parsed = new String(response.data, "UTF-8");
           // parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }
}